package com.cerosoft.handler;

import java.util.ArrayList;
import java.util.List;

import com.cerosoft.api.http.models.MediaInfo;
import com.cerosoft.api.http.models.MediaInfo.FileType;
import com.cerosoft.api.http.models.MediaInfo.MediaType;

public class HandlerDispatcher {

	public static IMediaHandler dispatch(MediaInfo info) {
		FileType fileType = info.getFileType();
		if (fileType == null) {
			return null;
		}
		switch (fileType) {
		case binary:
		case document: {
			return new FileHandler();
		}
		case html: {
			return new WebPageHandler();
		}
		case image: {
			return new ImageHandler();
		}
		case audio: {
			return new AudioHandler();
		}
		case video: {
			return new VideoHandler();
		}
		}
		return null;
	}

	public static String[] getStringArrayByType(List<MediaInfo> folderList,
			FileType fType) {
		List<String> res = new ArrayList<String>();
		String[] strs = null;
		if (folderList == null || folderList.size() == 0){
			strs = new String[0];
			return res.toArray(strs);
		}
		for (MediaInfo info : folderList) {
			MediaType type = info.getMediaType();
			if (type == null || info.getMediaType() != MediaType.file) {
				continue;
			} else {
				FileType fileType = info.getFileType();
				if (fileType == null || fType != fileType) {
					continue;
				}
				res.add(info.getPath());
			}
		}
		strs = new String[res.size()];
		return res.toArray(strs);
	}

}
